package api.actions;

import java.awt.Color;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import api.Interface;
import api.dialogs.AvisoInitDialog;
import api.dialogs.InicializarDialog;

public class Inicializar {
	
	public static void run() {
		final InicializarDialog dialog = new InicializarDialog();
		final AvisoInitDialog aidialog = new AvisoInitDialog();		

		dialog.spCols.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				int nuevo = (Integer) dialog.spCols.getValue() - 1;
				dialog.spinnerFY.setModel(new SpinnerNumberModel(0, 0, nuevo, 1));
				dialog.spinnerIY.setModel(new SpinnerNumberModel(0, 0, nuevo, 1));
			}
		});
			
			
		dialog.spFilas.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				int nuevo = (Integer) dialog.spFilas.getValue() - 1;
				dialog.spinnerFX.setModel(new SpinnerNumberModel(0, 0, nuevo, 1));
				dialog.spinnerIX.setModel(new SpinnerNumberModel(0, 0, nuevo, 1));
			}
		});

		dialog.spinnerFX.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				int fx = (Integer)dialog.spinnerFX.getValue();
				int ix = (Integer)dialog.spinnerIX.getValue();
				int fy = (Integer)dialog.spinnerFY.getValue();
				int iy = (Integer)dialog.spinnerIY.getValue();
				
				if ((iy == fy) && (fx == ix)) {
					dialog.btnAceptar.setEnabled(false);
					dialog.lblError.setForeground(Color.red);
				} else {
					dialog.btnAceptar.setEnabled(true);
					dialog.lblError.setForeground(new Color(238,238,238));
				}
			}
		});
		
		dialog.spinnerFY.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				int fx = (Integer)dialog.spinnerFX.getValue();
				int ix = (Integer)dialog.spinnerIX.getValue();
				int fy = (Integer)dialog.spinnerFY.getValue();
				int iy = (Integer)dialog.spinnerIY.getValue();
				
				if ((iy == fy) && (fx == ix)) {
					dialog.btnAceptar.setEnabled(false);
					dialog.lblError.setForeground(Color.red);
				} else {
					dialog.btnAceptar.setEnabled(true);
					dialog.lblError.setForeground(new Color(238,238,238));
				}
			}
		});
		
		dialog.spinnerIY.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				int fx = (Integer)dialog.spinnerFX.getValue();
				int ix = (Integer)dialog.spinnerIX.getValue();
				int fy = (Integer)dialog.spinnerFY.getValue();
				int iy = (Integer)dialog.spinnerIY.getValue();
				
				if ((iy == fy) && (fx == ix)) {
					dialog.btnAceptar.setEnabled(false);
					dialog.lblError.setForeground(Color.red);
				} else {
					dialog.btnAceptar.setEnabled(true);
					dialog.lblError.setForeground(new Color(238,238,238));
				}
			}
		});
		
		dialog.spinnerIX.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				int fx = (Integer)dialog.spinnerFX.getValue();
				int ix = (Integer)dialog.spinnerIX.getValue();
				int fy = (Integer)dialog.spinnerFY.getValue();
				int iy = (Integer)dialog.spinnerIY.getValue();
				
				if ((iy == fy) && (fx == ix)) {
					dialog.btnAceptar.setEnabled(false);
					dialog.lblError.setForeground(Color.red);
				} else {
					dialog.btnAceptar.setEnabled(true);
					dialog.lblError.setForeground(new Color(238,238,238));
				}
			}
		});
		
		dialog.btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int fx = (Integer)dialog.spinnerFX.getValue();
				int ix = (Integer)dialog.spinnerIX.getValue();
				int fy = (Integer)dialog.spinnerFY.getValue();
				int iy = (Integer)dialog.spinnerIY.getValue();
				
				if ((iy == fy) && (fx == ix)) {
					dialog.btnAceptar.setEnabled(false);
					dialog.lblError.setForeground(Color.red);
				} else {
					dialog.setVisible(false);
					Interface.smnObs.setEnabled(true);
					Interface.mniInicializar.setEnabled(false);
					Interface.mnAbrir.setEnabled(false);
					Interface.getTablero().setM((Integer)dialog.spFilas.getValue());
					Interface.getTablero().setN((Integer)dialog.spCols.getValue());
					Interface.getTablero().getRobot().setPosActual(new Point(ix, iy));
					Interface.getTablero().getRobot().setPosFinal(new Point(fx, fy));
					Interface.getTablero().getRobot().setPosInicial(new Point(ix, iy));
					
					int dimx = (int) Interface.getTablero().getM();
					int dimy = (int) Interface.getTablero().getN();
					
					aidialog.labelfin.setText("["+fx+", "+fy+"]");
					aidialog.labelini.setText("["+ix+", "+iy+"]");
					aidialog.lbldim.setText("["+dimx+", "+dimy+"]");
					
					aidialog.setVisible(true);
				}
			}
		});
		
		aidialog.btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				aidialog.setVisible(false);
			}
		});
		
		
		
		dialog.setVisible(true);
	}
}
